local S = mobs.intllib

local master_types = {

	{	nodes = {"default:stone_with_mese"},
		skins = {"mobs_dungeon_master.png"},
		master = true
	},

	{	nodes = {"nether:rack"},
		skins = {"mobs_dungeon_master_nether.png", "mobs_dungeon_master_nether2.png", "mobs_dungeon_master_nether3.png"},
		master2 = true
	},

	{	nodes = {"nether:rack_deep"},
		skins = {"mobs_dungeon_master_netherdeep.png", "mobs_dungeon_master_netherdeep2.png"},
		master3 = true
	},

	{	nodes = {"default:stone_with_diamond"},
		skins = {"mobs_dungeon_master4.png", "mobs_dungeon_master5.png"},
		master4 = true
	},
}


-- Dungeon Master by PilzAdam

mobs:register_mob("mobs_monster:dungeon_master", {
	type = "monster",
	attack_monsters = false,
	passive = false,
	damage = 10,
	attack_type = "dogshoot",
	dogshoot_switch = 1,
	dogshoot_count_max = 30,
	dogshoot_count2_max = 10,
	reach = 2,
	shoot_interval = 2.5,
	arrow = "mobs_monster:fireball",
	shoot_offset = 1.25,
	hp_min = 200,
	hp_max = 250,
	armor = 30,
	collisionbox = {-0.7, -1, -0.7, 0.7, 1.6, 0.7},
	visual = "mesh",
	mesh = "mobs_dungeon_master.b3d",
	textures = {
		{"mobs_dungeon_master2.png"},
		{"mobs_dungeon_master3.png"},
	},
	makes_footstep_sound = true,
	sounds = {
		random = "mobs_dungeonmaster",
		shoot_attack = "mobs_fireball",
		distance = 25,
	},
	walk_velocity = 1,
	run_velocity = 2,
	knock_back = false,
	jump = true,
	view_range = 25,
	drops = {
		{name = "default:mese_crystal_fragment", chance = 1, min = 11, max = 26},
		{name = "mobs:leather", chance = 1, min = 3, max = 5},
		{name = "default:mese_crystal", chance = 1, min = 2, max = 4},
		{name = "default:diamond", chance = 1, min = 3, max = 4},
		{name = "default:diamondblock", chance = 1, min = 1, max = 3},
		{name = "default:mese", chance = 1, min = 1, max = 2},
	},
	water_damage = 0,
	lava_damage = 10,
	light_damage = 0,
	fear_height = 5,
	animation = {
		stand_start = 0,
		stand_end = 19,
		walk_start = 20,
		walk_end = 35,
		punch_start = 36,
		punch_end = 48,
		shoot_start = 36,
		shoot_end = 48,
		speed_normal = 15,
		speed_run = 20,
	},
	
	immune_to = {
		{"nether:sword_nether", 0},
	},

	on_spawn = function(self)

		local pos = self.object:get_pos() ; pos.y = pos.y - 1
		local tmp

		for n = 1, #master_types do

			tmp = master_types[n]

			-- near node check
			if minetest.find_node_near(pos, 1, tmp.nodes) then
			
				self.base_texture = { tmp.skins[math.random(#tmp.skins)] }
				self.object:set_properties({textures = self.base_texture})
				
				-- check for master type and adjust settings
				if tmp.master then
					self.health = math.random(750, 1000)
					self.hp_max = 1000
					self.armor = 50
					self.reach = 3
					self.view_range = 35
					self.object:set_properties({
						collisionbox = {-1.2, -1.6, -1.2, 1.2, 2.4, 1.2},
						visual_size = {x = 1.50, y = 1.50}
					})
					
					-- override arrow settings
					self.arrow_override = function(arrow)
						arrow.damage_groups = 25
						arrow.object:set_properties({
							textures = {"mobs_fireball.png"},
							visual_size = {x = 1.50, y = 1.50}
					})
				end
			
				elseif tmp.master2 then
					self.armor = 5
					self.lava_damage = 0
					self.fire_damage = -2
					self.immune_to = {
						{"nether:axe_nether", 1},
						{"nether:sword_nether", 1},
					}
					
					self.arrow_override = function(arrow)
						arrow.damage_groups = 18
						arrow.object:set_properties({
							textures = {"mobs_fireball.png"}
					})
				end
				
				elseif tmp.master3 then
					self.lava_damage = 0
					self.fire_damage = 0
					self.armor = 5
					self.immune_to = {
						{"nether:axe_nether", 1},
						{"nether:sword_nether", 1},
					}

					self.arrow_override = function(arrow)
						arrow.tail_texture = "mobs:fireball4.png"
						arrow.object:set_properties({
							textures = {"mobs_fireball4.png"}
						})
					end

				elseif tmp.master4 then
				
					self.arrow_override = function(arrow)
						arrow.object:set_properties({
							textures = {"mobs_fireball3.png"}
						})
					end

					self.object:set_properties({
						visual_size = {x = 0.80, y = 0.80},
						collisionbox = {-0.7, -0.8, -0.7, 0.7, 1.3, 0.7}
					})
				end
			end
		end
	end,
})


if not mobs.custom_spawn_monster then
mobs:spawn({
	name = "mobs_monster:dungeon_master",
	nodes = {"default:stone", "default:stone_with_mese", "nether:rack", "nether:rack_deep", "default:stone_with_diamond"},
	max_light = 3,
	chance = 9000,
	active_object_count = 3,
	max_height = -5000,
})
end

mobs:register_egg("mobs_monster:dungeon_master", S("Dungeon Master"), "fire_basic_flame.png", 1)


mobs:alias_mob("mobs:dungeon_master", "mobs_monster:dungeon_master")

mobs:register_arrow("mobs_monster:fireball", {
	visual = "sprite",
	visual_size = {x = 1.25, y = 1.25},
	textures = {"mobs_fireball2.png"},
	collisionbox = {-0.1, -0.1, -0.1, 0.1, 0.1, 0.1},
	velocity = 15,
	tail_texture = "mobs_fireball2.png",
	tail = 10,
	tail_size = 10,
	glow = 5,
	expire = 0.1,

	on_activate = function(self, staticdata, dtime_s)
	
		-- make fireball indestructable
		self.object:set_armor_groups({immortal = 1, fleshy = 100})
	end,

	-- if player has a good weapon with 7+ damage it can deflect fireball
	on_punch = function(self, hitter, tflp, tool_capabilities, dir)

		if hitter and hitter:is_player() and tool_capabilities and dir then

			local damage = tool_capabilities.damage_groups and
				tool_capabilities.damage_groups.fleshy or 1

			local tmp = tflp / (tool_capabilities.full_punch_interval or 1.4)

			if damage > 6 and tmp < 4 then

				self.object:set_velocity({
					x = dir.x * self.velocity,
					y = dir.y * self.velocity,
					z = dir.z * self.velocity,
				})
			end
		end
	end,

	hit_player = function(self, player)

		local ent = self.object:get_luaentity()
		print("--damage", ent.damage)

		player:punch(self.object, 1.0, {
			full_punch_interval = 1.0,
			damage_groups = {fleshy = self.damage_groups or 12},
		}, nil)
	end,

	hit_mob = function(self, player)
		player:punch(self.object, 1.0, {
			full_punch_interval = 1.0,
			damage_groups = {fleshy = self.damage_groups or 8},
		}, nil)
	end,

	hit_node = function(self, pos, node)
		mobs:boom(self, pos, 2)
	end
		
})